package com.example.capsule_kafka.service;

import com.example.capsule_kafka.mapper.UserToImeiBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @Author: zhuzhiming
 * @Date: 2022/5/3 20:06
 */
@Service
public class UserToImeiService {

    @Autowired
    UserToImeiBeanMapper userToImeiBeanMapper;

    @Autowired
    RedisTemplate<String, String> redisTemplate;



    public String getUserIdByImei(String imei) {

        if (imei == null || imei.equals("")) {
            return "";
        }

        if (redisTemplate.opsForValue().get(imei) != null) {
            return redisTemplate.opsForValue().get(imei);
        }

        String userId = userToImeiBeanMapper.getUserIdByImei(imei);
        if (userId == null) {
            return "";
        }
        redisTemplate.opsForValue().set(imei, userId,30, TimeUnit.MINUTES);
        return userId;
    }

}
